perm filename A02.TEX[106,PHY] blob sn#807705 filedate 1985-09-20 generic text, type C, neo UTF8
COMMENT āŠ—   VALID 00005 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	\input macro.tex
C00018 00003	\tt
C00025 00004	[PHOTO:  Recording initiated  Mon 15-Nov-82 1:15AM]
C00032 00005	(****************************************************************************)
C00043 ENDMK
CāŠ—;
\input macro.tex
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\baselineskip 14pt
\rm
\def\qq{\qquad}
\def\q{\quad}

\line{\sevenrm a02.tex[106,phy] \today\hfill}

\baselineskip10pt
\tt
\smallskip
%(****************************************************************************)
%(*                          CS106-3,6 Homework #7                           *)
%(*  prof. R. Floyd                                          Stanford Univ.  *)
%(*  Author : Jerry Chu                                      Nov. 12, 1982   *)
%(*--------------------------------------------------------------------------*)
%(*     This program computes the value 'Pi' to 100 decimal places. By       *)
%(*  changing the constant 'size', one can get more digits of accuracy.     *)
%(*     The program has a debugger in it so that when the user was designing *)
%(*  the program, he can use the debugger to test if certain part of his     *)
%(*  program is well done.                                                   *)
%(****************************************************************************)
\line{CS106--3, 6 Homework \#7\hfill}
\line{Prof.\ R. Floyd\hfill}
\line{Author: Jerry Chu\hfill Nov.\ 12, 1982}

\medskip
{\rm This program computes the value of pi  to 100 decimal places. By      
changing the constant `size', one can get more digits of accuracy.
The program has features to facilitate debugging by separate testing
of subalgorithms.}

\parindent 0pt
\bigskip
\halign{\lft{#}\cr
PROGRAM Pi(input:/,output);\cr
LABEL\cr
\q  9999;\cr
CONST\cr
\q  size = 20;\cr
\q  scale = 100000; (* the array type is an integer from 0 to scale *)\cr
\q  debug = true; (* specify whether to debug or not *)\cr
TYPE\cr
\q  longreal = ARRAY[0..size] OF integer;\cr
VAR\cr
\q  pi,arctan1,arctan2 : longreal;\cr
\q  numofterm,i : integer;\cr}

\halign{\lft{#}\cr
PROCEDURE Arrayadd(a,b : longreal; VAR c : longreal);\cr
(* Add up two arrays a and b, put the result in c. *)\cr
\q  VAR\cr
\qq	i,carry,sum : integer;\cr
\q  BEGIN\cr
\q  carry := 0;\cr
\q  FOR i := size DOWNTO 0 DO BEGIN\cr
\qq     sum := a[i] + b[i] + carry;\cr
\qq     c[i] := sum MOD scale;\cr
\qq     carry := sum DIV scale;\cr
\qq     END; (* for *)\cr
\q  IF carry > 0 THEN BEGIN\cr
\qq	Writeln(' Overflow error stops ! (Real number exceeds',scale : 7,')');\cr
\qq	GOTO 9999;\cr
\qq	END; (* if *)\cr
\q  END; (* Arrayadd *)\cr}

\halign{\lft{#}\cr
PROCEDURE Arraysubt(a,b : longreal; VAR c : longreal);\cr
(* Subtract b from a, put the result in c. *)\cr
\q  VAR\cr
\qq	i,borrow : integer;\cr
\q  BEGIN\cr
\q  borrow := 0;\cr
\q  FOR i := size DOWNTO 0 DO BEGIN\cr
\qq	IF a[i]-borrow >= b[i] THEN BEGIN\cr
\qq\q       c[i] := a[i]-borrow-b[i];\cr
\qq\q       borrow := 0;\cr
\qq\q       END\cr
\qq	ELSE BEGIN\cr
\qq\q       c[i] := scale + a[i]-borrow-b[i];\cr
\qq\q       borrow := 1;\cr
\qq\q       END; (* else *)\cr
\qq	END; (* for *)\cr
\q  IF borrow = 1 THEN BEGIN\cr
\qq	Writeln('Underflow error stops ! (Real number less than 0)');\cr
\qq	GOTO 9999\cr
\qq	END; (* if *)\cr
\q  END; (* Arraysubt *)\cr}

\halign{\lft{#}\cr
PROCEDURE Arraydivision(a : longreal; b : integer; VAR c : longreal);\cr
(* Divide a array by an integer b, put the result in c array. *)\cr
\q  VAR\cr
\qq	i,remainder : integer;\cr
\q  BEGIN\cr
\q  IF b = 0 THEN BEGIN\cr
\qq	Writeln('Divided by zero error !');\cr
\qq	GOTO 9999;\cr
\qq	END\cr
\q  ELSE BEGIN\cr
\qq	remainder := 0;\cr
\qq	FOR i := 0 TO size DO BEGIN\cr
\qq\q	    c[i] := (a[i]+remainder*scale) DIV b;\cr
\qq\q	    remainder := (a[i]+remainder*scale) MOD b;\cr
\qq\q	    END; (* for **)\cr
\qq	IF remainder > (b DIV 2) THEN c[size] := c[size]+1;\cr
\qq	END; (* else *)\cr
\q  END; (* Arraydivision *)\cr}

\halign{\lft{#}\cr
FUNCTION Checkzero(a : longreal) : boolean;\cr
(* Check if the real number represented by array a is equal to zero. *)\cr
\q  VAR\cr
\qq	i : integer;\cr
\qq	zero : boolean;\cr
\q  BEGIN\cr
\q  zero := true;\cr
\q  i := 0;\cr
\q  WHILE zero AND (i <= size) DO BEGIN\cr
\qq	zero := (a[i] = 0);\cr
\qq	i := i+1;\cr
\qq	END; (* while *)\cr
\q  Checkzero := zero;\cr
\q  END; (* Checkzero *)\cr}

\halign{\lft{#}\cr
PROCEDURE Write\_longreal(a : longreal);\cr
(* Write the value represented by array a. *)\cr
\q  VAR\cr
\qq	i,j,k : integer;\cr
\q  FUNCTION Width(i : integer) : integer;\cr
\qq	VAR\cr
\qq\q	    k : integer;\cr
\qq	BEGIN\cr
\qq	k := 0;\cr
\qq	REPEAT\cr
\qq\q	    i := i DIV 10;\cr
\qq\q	    k := k+1;\cr
\qq	UNTIL i = 0;\cr
\qq	Width := k;\cr
\qq	END; (* width *)\cr
\q  BEGIN (* Write\_longreal *)\cr
\q  Write(a[0] : 5,'.');\cr
\q  k := Width(scale)-1;\cr
\q  FOR i := 1 TO size DO BEGIN\cr
\qq	FOR j := 1 TO k-Width(a[i]) DO Write('0');\cr
\qq	Write(a[i] : 1,' ');\cr
\qq	END; (* for *)\cr
\q  Writeln;\cr
\q  END; (* Write\_longreal *)\cr}

\halign{\lft{#}\cr
PROCEDURE Arctan(t: integer; VAR y : longreal; VAR n : integer);\cr
(* Compute arctan(1/t) by the series given in the handout. Put the result *)\cr
(* in y. When return, n equal to the number of terms being summed. *)\cr
\q  VAR
\qq	x,term : longreal;  ($\ast$ x holds the n-th power of (1/t). $\ast$)\cr
\hskip 70pt		    ($\ast$ term holds x/n. $\ast$)\cr
\q  BEGIN\cr
\q  x[0] := 1;\cr
\q  FOR i := 1 TO size DO x[i] := 0;\cr
\q  Arraydivision(x,t,x);\cr
\q  FOR i := 0 TO size DO BEGIN\cr
\qq	term[i] := x[i];\cr
\qq	y[i] := 0;\cr
\qq	END; (* for *)\cr
\q  n := 0;\cr
\q  WHILE NOT Checkzero(term) DO BEGIN (* the terminating condition is when *)\cr
\hskip 70pt			       ($ast$ the computed term equal to 0 $\ast$)\cr
\qq	n := n+1;\cr
\qq	IF (n MOD 2)=0 THEN Arraysubt(y,term,y)\cr
\qq	ELSE Arrayadd(y,term,y);\cr
\qq	Arraydivision(x,t,x);\cr
\qq	Arraydivision(x,t,x);\cr
\qq	Arraydivision(x,2*n+1,term);\cr
\qq	END; (* while *)\cr
\q  END; (* Arctan *)\cr}

\halign{\lft{#}\cr
PROCEDURE Debugger;\cr
(* Test several procedures using data given by the user from the terminal. *)\cr
(* this procedure makes debugging of the program much easier. *)\cr
\q  VAR\cr
\qq	i,j : integer;\cr
\qq	a,b : longreal;\cr
\q  BEGIN\cr
\q  Writeln('Please give A and B initial values.');\cr
\q  Writeln('A :=');\cr
\q  Readln;\cr
\q  Read(a[0]);\cr
\q  Writeln('B :=');\cr
\q  Readln;\cr
\q  Read(b[0]);\cr
\q  FOR i := 1 TO size DO BEGIN\cr
\qq	a[i] := 0;\cr
\qq	b[i] := 0;\cr
\qq	END;\cr
\q  i := 1;\cr
\q  WHILE i <> 0 DO BEGIN\cr
\qq	Writeln('Please enter four choices of testing :');\cr
\qq	Writeln(' 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B');\cr
\qq	Readln;\cr
\qq	Read(i);\cr
\qq	CASE i OF\cr
\qq\q	    1 : BEGIN\cr
\qq\q\phantom{1 : }	Writeln('A <== A + B ');\cr
\qq\q\phantom{1 : }	Arrayadd(a,b,a);\cr
\qq\q\phantom{1 : }	Write\_longreal(a);\cr
\qq\q\phantom{1 : }	END;\cr
\qq\q	    2 : BEGIN\cr
\qq\q\phantom{2 : }	Writeln('A <== A - B');\cr
\qq\q\phantom{2 : }	Arraysubt(a,b,a);\cr
\qq\q\phantom{2 : }	Write\_longreal(a);\cr
\qq\q\phantom{2 : }	END;\cr
\qq\q	    3 : BEGIN\cr
\qq\q\phantom{3 : }	Writeln('Please enter divisor.');\cr
\qq\q\phantom{3 : }	Readln;\cr
\qq\q\phantom{3 : }	Read(j);\cr
\qq\q\phantom{3 : }	Writeln('A <== A / divisor');\cr
\qq\q\phantom{3 : }	Arraydivision(a,j,a);\cr
\qq\q\phantom{3 : }	Write\_longreal(a);\cr
\qq\q\phantom{3 : }	END;\cr
\qq\q	    4 : BEGIN\cr
\qq\q\phantom{4 : }	Writeln('Please enter divisor.');\cr
\qq\q\phantom{4 : }	Readln;\cr
\qq\q\phantom{4 : }	Read(j);\cr
\qq\q\phantom{4 : }	Writeln('B <== B / divisor');\cr
\qq\q\phantom{4 : }	Arraydivision(b,j,b);\cr
\qq\q\phantom{4 : }	Write\_longreal(b);\cr
\qq\q\phantom{4 : }	END;\cr
\qq\q	    OTHERS :\cr
\qq	END; (* case *)\cr
\qq	END; (* while *)\cr
\q  END; (* Debugger *)\cr
\noalign{\vskip 3pt}
BEGIN (* Main *)\cr
IF debug THEN Debugger;\cr
Writeln('The following computation uses the power series :');\cr
Writeln('\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 3\ \ \ \ \ \ 5\ \ \ \ \ \ 7 ');\cr
Writeln('arctan(x) = x - x /3 + x /5 - x /7 + .....');\cr
Writeln('to compute Pi, the computed result has',5*size : 5,' accurate digits !');\cr
Writeln;\cr
numofterm := 0;\cr
Arctan(239,arctan2,numofterm);\cr
Writeln('ArcTan(1/239) =');\cr
Write\_longreal(arctan2);\cr
Writeln('The number of terms being computed in the series is ',numofterm:1);\cr
Writeln('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --');\cr
Writeln;\cr
Arctan(5,arctan1,numofterm);\cr
Writeln('ArcTan(1/5)  =');\cr
Write\_longreal(arctan1);\cr
Writeln('The number of terms being computed in the series is ',numofterm:1);\cr
Writeln('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --');\cr
Writeln;\cr
FOR i := 1 TO 2 DO Arrayadd(arctan1,arctan1,arctan1);\cr
Arraysubt(arctan1,arctan2,pi);\cr
FOR i := 1 TO 2 DO Arrayadd(pi,pi,pi);\cr
Writeln('Pi =');\cr
Write\_longreal(pi);\cr
9999 :\cr
(* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- *)\cr
END. (* Main *)\cr}

\vfill\eject
\tt
\halign{\lft{#}\cr
[PHOTO: Recording initiated Mon 15-Nov-82 1:15AM]\cr
\noalign{\vskip 3pt}
@exECUTE (FROM) hw7.pgo\cr
Stanford LOTS/Passgo 20 [PI   ] -- 1\cr
Runtime: 0; 0.824\cr
[PI execution]\cr
INPUT :\cr
OUTPUT :\cr
Please give A and B initial values.\cr
A :=\cr
5\cr
B :=\cr
2\cr
Please enter four choices of testing :\cr
\quad 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B\cr
1\cr
A <== A + B\cr
\qquad 7.00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000\cr
00000 00000 00000 00000 00000 00000 00000 00000\cr
Please enter four choices of testing :\cr
\quad 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B\cr
3\cr
Please enter divisor.\cr
3\cr
A <== A / divisor\cr
\qquad 2.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333\cr
33333 33333 33333 33333 33333 33333 33333 33333\cr
Please enter four choices of testing :\cr
\quad 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B\cr
4\cr
Please enter divisor.\cr
3\cr
B <== B / divisor\cr
\qquad 0.66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 66666\cr
66666 66666 66666 66666 66666 66666 66666 66667\cr
Please enter four choices of testing :\cr
\quad 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B\cr
1\cr
A <== A + B\cr
\qquad 3.00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000\cr
00000 00000 00000 00000 00000 00000 00000 00000\cr
Please enter four choices of testing :\cr
\quad 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B\cr
2\cr
A <== A - B\cr
\qquad 2.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333\cr
33333 33333 33333 33333 33333 33333 33333 33333\cr}

\vfill\eject

\halign{\lft{#}\cr
Please enter four choices of testing :\cr
\quad 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B\cr
0\cr
The following computation uses the power series:\cr
\qq\qq\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 3\ \ \ \ \ \ 5\ \ \ \ \ \ 7\cr
\qq\qq arctan(x) = x - x /3 + x /5 - x /7 + ...\cr}

\halign{\lft{#}\cr
to compute Pi, the computed result has 100 accurate digits !\cr
ArcTan(1/239) =\cr
\qquad  0.00418 40760 02074 72386 45382 14959 28545 27410 48065 30763 19508 27019\cr
61288 71817 78341 42289 32737 82605 81362 29092 \cr
The number of terms being computed in the series is 21\cr
\noalign{\vskip 5pt}
ArcTan(1/5) =\cr
\qquad  0.19739 55598 49880 75837 00497 65194 79029 34475 85103 78785 21015 17688\cr
94024 10339 69978 24378 57326 97828 03728 80439 \cr
The number of terms being computed in the series is 70\cr
\noalign{\vskip 5pt}
Pi =
\qquad 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944\cr
59230 78164 06286 20899 86280 34825 34211 70656 \cr
\noalign{\vskip 5pt}
The following computation uses the power series:\cr
\qq\qq\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 3\ \ \ \ \ \ 5\ \ \ \ \ \ 7\cr
\qq\qq arctan(x) = x - x /3 + x /5 - x /7 + ...\cr}


\halign{\lft{#}\cr
to compute Pi, the computed result has 1000 accurate digits !\cr
\noalign{\vskip 3pt}
The number of terms being computed in the series is 713.\cr
\noalign{\vskip 5pt}
Pi =\cr
\qquad 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944\cr
59230 78164 06286 20899 86280 34825 34211 70679 82148 08651 32823 06647 09384 \cr
46095 50582 23172 53594 08128 48111 74502 84102 70193 85211 05559 64462 29489\cr
54930 38196 44288 10975 66593 34461 28475 64823 37867 83165 27120 19091 45648\cr 
56692 34603 48610 45432 66482 13393 60726 02491 41273 72458 70066 06315 58817\cr
48815 20920 96282 92540 91715 36436 78925 90360 01133 05305 48820 46652 13841\cr
46951 94151 16094 33057 27036 57595 91953 09218 61173 81932 61179 31051 18548\cr
07446 23799 62749 56735 18857 52724 89122 79381 83011 94912 98336 73362 44065\cr
66430 86021 39494 63952 24737 19070 21798 60943 70277 05392 17176 29317 67523\cr
84674 81846 76694 05132 00056 81271 45263 56082 77857 71342 75778 96091 73637\cr
17872 14684 40901 22495 34301 46549 58537 10507 92279 68925 89235 42019 95611\cr
21290 21960 86403 44181 59813 62977 47713 09960 51870 72113 49999 99837 29780\cr
49951 05973 17328 16096 31859 50244 59455 34690 83026 42522 30825 33446 85035\cr
26193 11881 71010 00313 78387 52886 58753 32083 81420 61717 76691 47303 59825\cr
34904 28755 46873 11595 62863 88235 37875 93751 95778 18577 80532 17122 68066\cr
13001 92787 66111 95909 21642 01908 \cr}

\vfill\eject\end
[PHOTO:  Recording initiated  Mon 15-Nov-82 1:15AM]

@exECUTE (FROM) hw7.pgo
Stanford LOTS/Passgo 20 [PI    ] -- 1.. 
Runtime:   0: 0.824
[PI     execution]
INPUT      : 
OUTPUT     : 
Please give A and B initial values.
A :=
5
B :=
2
Please enter four choices of testing :
 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B
1
A <== A + B 
    7.00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00
000 00000 00000 00000 00000 00000 00000 00000 
Please enter four choices of testing :
 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B
3
Please enter divisor.
3
A <== A / divisor
    2.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33
333 33333 33333 33333 33333 33333 33333 33333 
Please enter four choices of testing :
 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B
4
Please enter divisor.
3
B <== B / divisor
    0.66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 66
666 66666 66666 66666 66666 66666 66666 66667 
Please enter four choices of testing :
 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B
1
A <== A + B 
    3.00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00
000 00000 00000 00000 00000 00000 00000 00000 
Please enter four choices of testing :
 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B
2
A <== A - B
    2.33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33333 33
333 33333 33333 33333 33333 33333 33333 33333 
Please enter four choices of testing :
 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B
0
The following computation uses the power series :
                        3      5      7 
	arctan(x) = x - x /3 + x /5 - x /7 + .....
to compute Pi, the computed result has  100 accurate digits !

ArcTan(1/239) =
    0.00418 40760 02074 72386 45382 14959 28545 27410 48065 30763 19508 27019 61
288 71817 78341 42289 32737 82605 81362 29092 
The number of terms being computed in the series is 21
-------------------------------------------------------------------------------

ArcTan(1/5)  =
    0.19739 55598 49880 75837 00497 65194 79029 34475 85103 78785 21015 17688 94
024 10339 69978 24378 57326 97828 03728 80439 
The number of terms being computed in the series is 70
-------------------------------------------------------------------------------

Pi =
    3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59
230 78164 06286 20899 86280 34825 34211 70656 

-------------------------------------------------------------------------------

The following computation uses the power series :
                        3      5      7 
	arctan(x) = x - x /3 + x /5 - x /7 + .....
to compute Pi, the computed result has 1000 accurate digits !

The number of terms being computed in the series is 713.
-------------------------------------------------------------------------------

Pi =
    3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59
230 78164 06286 20899 86280 34825 34211 70679 82148 08651 32823 06647 09384 4609
5 50582 23172 53594 08128 48111 74502 84102 70193 85211 05559 64462 29489 54930 
38196 44288 10975 66593 34461 28475 64823 37867 83165 27120 19091 45648 56692 34
603 48610 45432 66482 13393 60726 02491 41273 72458 70066 06315 58817 48815 2092
0 96282 92540 91715 36436 78925 90360 01133 05305 48820 46652 13841 46951 94151 
16094 33057 27036 57595 91953 09218 61173 81932 61179 31051 18548 07446 23799 62
749 56735 18857 52724 89122 79381 83011 94912 98336 73362 44065 66430 86021 3949
4 63952 24737 19070 21798 60943 70277 05392 17176 29317 67523 84674 81846 76694 
05132 00056 81271 45263 56082 77857 71342 75778 96091 73637 17872 14684 40901 22
495 34301 46549 58537 10507 92279 68925 89235 42019 95611 21290 21960 86403 4418
1 59813 62977 47713 09960 51870 72113 49999 99837 29780 49951 05973 17328 16096 
31859 50244 59455 34690 83026 42522 30825 33446 85035 26193 11881 71010 00313 78
387 52886 58753 32083 81420 61717 76691 47303 59825 34904 28755 46873 11595 6286
3 88235 37875 93751 95778 18577 80532 17122 68066 13001 92787 66111 95909 21642 
01908 

(****************************************************************************)
(*                          CS106-3,6 Homework #7                           *)
(*  prof. R. Floyd                                          Stanford Univ.  *)
(*  Author : Jerry Chu                                      Nov. 12, 1982   *)
(*--------------------------------------------------------------------------*)
(*     This program computes the value 'Pi' to 100 decimal places. By       *)
(*  changing the constant 'size', one can get more digits of accuracy.     *)
(*     The program has a debugger in it so that when the user was designing *)
(*  the program, he can use the debugger to test if certain part of his     *)
(*  program is well done.                                                   *)
(****************************************************************************)
PROGRAM Pi(input:/,output);
LABEL
    9999;
CONST
    size = 20;		
    scale = 100000;	(*the array type is an integer from 0 to scale*)
    debug = true;	(*specify whether to debug or not	      *)
TYPE
    longreal = ARRAY[0..size] OF integer; 
VAR
    pi,arctan1,arctan2 : longreal;
    numofterm,i : integer;

PROCEDURE Arrayadd(a,b : longreal; VAR c : longreal);
(* Add up two arrays a and b, put the result in c. *)
    VAR
	i,carry,sum : integer;
    BEGIN
    carry := 0;
    FOR i := size DOWNTO 0 DO BEGIN
	sum := a[i] + b[i] + carry;
	c[i] := sum MOD scale;
	carry := sum DIV scale;
	END; (*for*)
    IF carry > 0 THEN BEGIN
	Writeln(' Overflow error stops ! (Real number exceeds',scale : 7,')');
	GOTO 9999;
	END; (*if*)
    END; (*Arrayadd*)

PROCEDURE Arraysubt(a,b : longreal; VAR c : longreal);
(* Subtract b from a, put the result in c. *)
    VAR
	i,borrow : integer;
    BEGIN
    borrow := 0;
    FOR i := size DOWNTO 0 DO BEGIN
	IF a[i]-borrow >= b[i] THEN BEGIN
	    c[i] := a[i]-borrow-b[i];
	    borrow := 0;
	    END
	ELSE BEGIN
	    c[i] := scale + a[i]-borrow-b[i];
	    borrow := 1;
	    END; (*else*)
	END; (*for*)
    IF borrow = 1 THEN BEGIN
	Writeln('Underflow error stops ! (Real number less than 0)');
	GOTO 9999
	END; (*if*)
    END; (*Arraysubt*)

PROCEDURE Arraydivision(a : longreal; b : integer; VAR c : longreal);
(* Divide a array by an integer b, put the result in c array. *)
    VAR
	i,remainder : integer;
    BEGIN
    IF b = 0 THEN BEGIN
	Writeln('Divided by zero error !');
	GOTO 9999;
	END
    ELSE BEGIN
	remainder := 0;
	FOR i := 0 TO size DO BEGIN
	    c[i] := (a[i]+remainder*scale) DIV b;
	    remainder := (a[i]+remainder*scale) MOD b;
	    END; (*for*)
	IF remainder > (b DIV 2) THEN c[size] := c[size]+1;
	END; (*else*)
    END; (*Arraydivision*)

FUNCTION Checkzero(a : longreal) : boolean;
(* Check if the real number represented by array a is equal to zero.*)
    VAR
	i : integer;
	zero : boolean;

    BEGIN
    zero := true;
    i := 0;
    WHILE zero AND (i <= size) DO BEGIN
	zero := (a[i] = 0);
	i := i+1;
	END; (*while*)
    Checkzero := zero;
    END; (*Checkzero*)

PROCEDURE Write_longreal(a : longreal);
(* Write the value represented by array a.*)
    VAR
	i,j,k : integer;

    FUNCTION Width(i : integer) : integer;
	VAR
	    k : integer;
	BEGIN
	k := 0;
	REPEAT
	    i := i DIV 10;
	    k := k+1;
	UNTIL i = 0;
	Width := k;
	END; (*width*)

    BEGIN (*Write_longreal*)
    Write(a[0] : 5,'.');
    k := Width(scale)-1;
    FOR i := 1 TO size DO BEGIN
	FOR j := 1 TO k-Width(a[i]) DO Write('0');
	Write(a[i] : 1,' ');
	END; (*for*)
    Writeln;
    END; (*Write_longreal*)

PROCEDURE Arctan(t: integer; VAR y : longreal; VAR n : integer);

(* Compute arctan(1/t) by the series given in the handout. Put the result *)
(* in y. When return, n equal to the number of terms being summed.        *)

    VAR
	x,term : longreal;  (* x holds the n-th power of (1/t). *)
			    (* term holds x/n.                  *)
    BEGIN
    x[0] := 1;
    FOR i := 1 TO size DO x[i] := 0;
    Arraydivision(x,t,x);
    FOR i := 0 TO size DO BEGIN
	term[i] := x[i];
	y[i] := 0;
	END; (*for*)
    n := 0;
    WHILE NOT Checkzero(term) DO BEGIN (* the terminating condition is when *)
				       (* the computed term equal to 0      *)
	n := n+1;
	IF (n MOD 2)=0 THEN Arraysubt(y,term,y)
	ELSE Arrayadd(y,term,y);
	Arraydivision(x,t,x);
	Arraydivision(x,t,x);
	Arraydivision(x,2*n+1,term);
	END; (*while*)
    END; (*Arctan*)

PROCEDURE Debugger;
(* Test several procedures using data given by the user from the terminal. *)
(* this procedure makes debugging of the program much easier.              *)
    VAR
	i,j : integer;
	a,b : longreal;

    BEGIN
    Writeln('Please give A and B initial values.');
    Writeln('A :=');
    Readln;
    Read(a[0]);
    Writeln('B :=');
    Readln;
    Read(b[0]);
    FOR i := 1 TO size DO BEGIN
	a[i] := 0;
	b[i] := 0;
	END;
    i := 1;
    WHILE i <> 0 DO BEGIN
	Writeln('Please enter four choices of testing :');
	Writeln(' 0 : quit;  1 : add;  2 : sub;  3 : div for A;  4 : div for B');
	Readln;
	Read(i);
	CASE i OF
	    1 : BEGIN
		Writeln('A <== A + B ');
		Arrayadd(a,b,a);
		Write_longreal(a);
		END;
	    2 : BEGIN
		Writeln('A <== A - B');
		Arraysubt(a,b,a);
		Write_longreal(a);
		END;
	    3 : BEGIN
		Writeln('Please enter divisor.');
		Readln;
		Read(j);
		Writeln('A <== A / divisor');
		Arraydivision(a,j,a);
		Write_longreal(a);
		END;
	    4 : BEGIN
		Writeln('Please enter divisor.');
		Readln;
		Read(j);
		Writeln('B <== B / divisor');
		Arraydivision(b,j,b);
		Write_longreal(b);
		END;
	    OTHERS :
	END; (*case*)
	END; (*while*)
    END; (*Debugger*)


BEGIN (*Main*)
IF debug THEN Debugger;
Writeln('The following computation uses the power series :');
Writeln('                        3      5      7 ');
Writeln('       arctan(x) = x - x /3 + x /5 - x /7 + .....');
Writeln('to compute Pi, the computed result has',5*size : 5,' accurate digits !');
Writeln;
numofterm := 0;
Arctan(239,arctan2,numofterm);
Writeln('ArcTan(1/239) =');
Write_longreal(arctan2);
Writeln('The number of terms being computed in the series is ',numofterm:1);
Writeln('-------------------------------------------------------------------------------');
Writeln;
Arctan(5,arctan1,numofterm);
Writeln('ArcTan(1/5)  =');
Write_longreal(arctan1);
Writeln('The number of terms being computed in the series is ',numofterm:1);
Writeln('-------------------------------------------------------------------------------');
Writeln;
FOR i := 1 TO 2 DO Arrayadd(arctan1,arctan1,arctan1);
Arraysubt(arctan1,arctan2,pi);
FOR i := 1 TO 2 DO Arrayadd(pi,pi,pi);
Writeln('Pi =');
Write_longreal(pi);
9999 :
(*--------------------------------------------------------------------------*)
END. (*Main*)